
//
//  VisitorView.swift
//  Swift微博
//
//  Created by 陈竹 on 16/2/22.
//  Copyright © 2016年 陈竹. All rights reserved.
//

import UIKit
import SnapKit

protocol VisitorViewDelegate:NSObjectProtocol{
///  访客视图注册
    func VisitorDidRegister()
/// 访客视图登陆
    func VisitorDidLogin()
}

class VisitorView: UIView {
    //MARK:-代理属性
    //代理一定要用weak
    weak var delegate:VisitorViewDelegate?

    //MARK:- 生命周期函数
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
     registerButton.addTarget(self, action: "didClickRegisterBtn", forControlEvents: .TouchUpInside)
        loginButton.addTarget(self, action: "didClickLoginBtn", forControlEvents: .TouchUpInside)
        
    }
    
    //手写必须实现此方法，作用:使用xib／sb设置界面会报错
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    //MARK:- 公共方法
    //设置访客视图
    func setupInfo(imageName: String? = nil,message : String){
        messageLabel.text=message
        guard let imageName = imageName else{
        startAnimation()
        return
        }
        homeIconView.hidden=true
        maskIconView.hidden=true
        
        iconView.image=UIImage(named: imageName)
    }
    
    
    //MARK:- 私有方法
    ///  设置动画
    private func startAnimation(){
        let anim=CABasicAnimation(keyPath: "transform.rotation")
        anim.toValue=2 * M_PI
        anim.repeatCount=MAXFLOAT
        anim.duration=20
        //屏幕离开不会销毁，父容器销毁才会随之销毁
        anim.removedOnCompletion=false
        iconView.layer.addAnimation(anim, forKey: nil)
    }
///  注册按钮点击事件
    @objc private func didClickRegisterBtn(){
        
     delegate?.VisitorDidRegister()
    }
///  登陆按钮点击事件
    @objc private func didClickLoginBtn(){
    delegate?.VisitorDidLogin()
    }
    
    //图标视图
    private lazy var iconView:UIImageView = UIImageView(image:UIImage( named:"visitordiscover_feed_image_smallicon"))
    //遮罩视图
    private lazy var maskIconView:UIImageView=UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    //小房子图标
    private lazy var homeIconView:UIImageView=UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    //消息标签
       private lazy var messageLabel=UILabel(text: "关注一些人，回这里看看有什么惊喜关注一些人，里看看有什么惊喜关注一些人", color: UIColor.redColor(), alignment: .Center)
    //注册按钮
    private lazy var registerButton=UIButton(title: "注册", color:UIColor.orangeColor() , backImageName: "common_button_white_disable")
    
    //登陆按钮
    private lazy var loginButton=UIButton(title: "登陆",backImageName: "common_button_white_disable")
}
// MARK: - 设置界面
extension VisitorView{
    private func setupUI(){
        
        
        addSubview(iconView)
        addSubview(maskIconView)
        addSubview(homeIconView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        //文字居中
        messageLabel.textAlignment = .Center
       
        //图标视图
        iconView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self)
            make.centerY.equalTo(self).offset(-60)
        }
        //小房子视图
        homeIconView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(iconView)
        }
        //提示文字
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView)
            make.top.equalTo(iconView.snp_bottom).offset(20)
            make.size.equalTo(CGSize(width: 225, height: 36))
            
        }
        
        //注册按钮
        registerButton.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(messageLabel.snp_bottom).offset(20)
            make.left.equalTo(messageLabel)
            make.size.equalTo(CGSize(width: 100, height: 36))
        }
        //登陆按钮
        loginButton.snp_makeConstraints { (make) -> Void in
            make.right.equalTo(messageLabel)
            make.size.equalTo(registerButton)
            make.top.equalTo(registerButton)
        }
        //遮罩视图
        maskIconView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(self)
            make.right.equalTo(self)
            make.top.equalTo(self)
            make.bottom.equalTo(registerButton)
        }

        //设置背景颜色
        backgroundColor=UIColor(white: 237.0/255.0, alpha: 1.0)
    }


}